/**
 * 
 */
package com.xbang.attendance.web.action.delivery.deliverylist;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.DeliveryDetails;
import com.xbang.attendance.entity.order.DeliveryOrder;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.service.DeliveryOrderService;
import com.xbang.attendance.service.ProductService;
import com.demo2do.lighturl.annotation.Action;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.struts.support.SearchActionSupport;

import enumeration.DeliveryStatus;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final Log logger = LogFactory.getLog(Search.class);

	private static final long serialVersionUID = -1120551442020217513L;

	@Autowired
	private DeliveryOrderService deliveryOrderService;

	private DeliveryOrder deliveryOrder;

	private List<DeliveryOrder> deliveryOrders = new ArrayList<DeliveryOrder>();

	private List<Region> regions = new ArrayList<Region>();

	private List<DeliveryStatus> deliveryStatus = new ArrayList<DeliveryStatus>();

	private List<DeliveryDetails> deliveryDetails = new ArrayList<DeliveryDetails>();

	private Integer id;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {

		regions = deliveryOrderService.getObjects(Region.class);

		deliveryStatus = deliveryOrderService.getObjects(DeliveryStatus.class);

		return "search";
	}

	@Action("/delivery/deliverydetails/${id}/search")
	public String detailSearch() throws Exception {
		regions = deliveryOrderService.getObjects(Region.class);

		deliveryStatus = deliveryOrderService.getObjects(DeliveryStatus.class);

		return "detail-search";
	}
	private String deliveryStatusString;
	@Action("/delivery/deliverydetails/${id}/search/do")
	public String doDetailSearch() throws Exception {
		if (id != null) {
			deliveryOrder = (DeliveryOrder) deliveryOrderService.get(DeliveryOrder.class, id);
			String hqlSentence = " FROM DeliveryDetails deliverydetails WHERE 1 = 1 ";
			searchComponent.addSearchElement("eq.deliverydetails.deliveryOrder.id", id);
			Result result = deliveryOrderService.searchByPage(hqlSentence, searchComponent, page);

			deliveryDetails = result.getItems();
			page = result.getPage();
		}
		return "detail";
	}

	@Action("/delivery/deliverylist/search/do")
	public String doSearch() throws Exception {

		String hqlSentence = " FROM DeliveryOrder deliveryorder WHERE 1 = 1 ";
		if(StringUtils.isNotEmpty(deliveryStatusString)){
			searchComponent.addSearchElement("eq.deliveryorder.deliveryStatus", DeliveryStatus.valueOf(deliveryStatusString));
			
		}
		if (searchComponent.getOrderParameters().isEmpty()){
			searchComponent.addSearchElement("orderBy.deliveryorder.deliveryDate", "DESC");
		}
		Result result = deliveryOrderService.searchByPage(hqlSentence, searchComponent, page);

		deliveryOrders = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public DeliveryOrderService getDeliveryOrderService() {
		return deliveryOrderService;
	}

	public void setDeliveryOrderService(DeliveryOrderService deliveryOrderService) {
		this.deliveryOrderService = deliveryOrderService;
	}

	public DeliveryOrder getDeliveryOrder() {
		return deliveryOrder;
	}

	public void setDeliveryOrder(DeliveryOrder deliveryOrder) {
		this.deliveryOrder = deliveryOrder;
	}

	public List<DeliveryOrder> getDeliveryOrders() {
		return deliveryOrders;
	}

	public void setDeliveryOrders(List<DeliveryOrder> deliveryOrders) {
		this.deliveryOrders = deliveryOrders;
	}

	public List<Region> getRegions() {
		return regions;
	}

	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	public List<DeliveryStatus> getDeliveryStatus() {
		return deliveryStatus;
	}

	public void setDeliveryStatus(List<DeliveryStatus> deliveryStatus) {
		this.deliveryStatus = deliveryStatus;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the deliveryDetails
	 */
	public List<DeliveryDetails> getDeliveryDetails() {
		return deliveryDetails;
	}

	/**
	 * @param deliveryDetails
	 *            the deliveryDetails to set
	 */
	public void setDeliveryDetails(List<DeliveryDetails> deliveryDetails) {
		this.deliveryDetails = deliveryDetails;
	}

	public String getDeliveryStatusString() {
		return deliveryStatusString;
	}

	public void setDeliveryStatusString(String deliveryStatusString) {
		this.deliveryStatusString = deliveryStatusString;
	}

}
